const reqlib = require('app-root-path').require
const btbAs = reqlib('/app/common/btb')
const numberUtil = reqlib('/app/util/numberUtil')

const checkMarketTag = (list, tag) => {
  if (tag === 'any') {
    return true
  }
  const map = {
    '-1': list[0],
    '0': !(list[0] || list[1]),
    '1': list[1]
  }
  if (Array.isArray(tag)) {
    let flag = false
    tag.forEach((v) => {
      // 如果是数组那就是只要一个达标就行了
      const vstr = `${v}`
      if (map[vstr]) {
        flag = true
      }
    })
    return flag
  }
  return map[`${tag}`]
}

function MarketMachine (config) {
  config.marketInfo = config.marketInfo || {}
  config.marketInfo.newHighRate = numberUtil.keepTwoDecimals((config.marketInfo.newHighCount || 0) / config.marketInfo.asCount)
  config.marketInfo.newLowRate = numberUtil.keepTwoDecimals((config.marketInfo.newLowCount || 0) / config.marketInfo.asCount)
  this.marketInfo = config.marketInfo
  this.marketInfoList = config.marketInfoList || []
}

MarketMachine.prototype = {
  getAllFlag () {
    return {
      indexIfMarketDownTrend: this.indexIfMarketDownTrend(),
      indexIfMarketUpTrend: this.indexIfMarketUpTrend(),
      // 市场没有方向
      ifMarketNoDirection: this.ifMarketNoDirection(),
      indexIfMarket15mDownTrend: this.indexIfMarket15mDownTrend(),
      indexIfMarket15mUpTrend: this.indexIfMarket15mUpTrend(),
      ifMarket1dAllGood: this.ifMarket1dAllGood(),
      ifMarket1dHasGood: this.ifMarket1dHasGood(),
      ifMarket1dHasBad: this.ifMarket1dHasBad(),
      ifMarket1dAllBad: this.ifMarket1dAllBad(),
      ifMarket1dMacdEmaHasGood: this.ifMarket1dMacdEmaHasGood(),
      ifMarket1dMacdEmaHasBad: this.ifMarket1dMacdEmaHasBad(),
      ifMarket1dMacdEmaAllGood: this.ifMarket1dMacdEmaAllGood(),
      ifMarket1dMacdEmaAllBad: this.ifMarket1dMacdEmaAllBad(),
      ifMarket4hHasGood: this.ifMarket4hHasGood(),
      ifMarket4hAllGood: this.ifMarket4hAllGood(),
      ifMarket4hHasBad: this.ifMarket4hHasBad(),
      ifMarket4hAllBad: this.ifMarket4hAllBad(),
      ifMarket4hMacdEmaAllGood: this.ifMarket4hMacdEmaAllGood(),
      ifMarket4hMacdEmaAllBad: this.ifMarket4hMacdEmaAllBad(),
      ifMarket4hMacdEmaHasGood: this.ifMarket4hMacdEmaHasGood(),
      ifMarket4hMacdEmaHasBad: this.ifMarket4hMacdEmaHasBad(),
      ifMarket1hAllGood: this.ifMarket1hAllGood(),
      ifMarket1hHasGood: this.ifMarket1hHasGood(),
      ifMarket1hHasBad: this.ifMarket1hHasBad(),
      ifMarket1hAllBad: this.ifMarket1hAllBad(),
      ifMarket1hMacdEmaHasGood: this.ifMarket1hMacdEmaHasGood(),
      ifMarket1hMacdEmaHasBad: this.ifMarket1hMacdEmaHasBad(),
      ifMarket1hMacdEmaAllGood: this.ifMarket1hMacdEmaAllGood(),
      ifMarket1hMacdEmaAllBad: this.ifMarket1hMacdEmaAllBad(),
      ifMarket15mAllGood: this.ifMarket15mAllGood(),
      ifMarket15mHasGood: this.ifMarket15mHasGood(),
      ifMarket15mHasBad: this.ifMarket15mHasBad(),
      ifMarket15mAllBad: this.ifMarket15mAllBad(),
      ifMarket15mMacdEmaHasGood: this.ifMarket15mMacdEmaHasGood(),
      ifMarket15mMacdEmaHasBad: this.ifMarket15mMacdEmaHasBad(),
      ifMarket15mMacdEmaAllGood: this.ifMarket15mMacdEmaAllGood(),
      ifMarket15mMacdEmaAllBad: this.ifMarket15mMacdEmaAllBad(),
      isMarketLittleGoodUp: this.isMarketLittleGoodUp(),
      isMarketMiniGoodUp: this.isMarketMiniGoodUp(),
      isMarketSmallGoodUp: this.isMarketSmallGoodUp(),
      isMarketBigGoodUp: this.isMarketBigGoodUp(),
      isMarketLittleBadDown: this.isMarketLittleBadDown(),
      isMarketMiniBadDown: this.isMarketMiniBadDown(),
      isMarketSmallBadDown: this.isMarketSmallBadDown(),
      isMarketBigBadDown: this.isMarketBigBadDown(),
      isMarketChaos: this.isMarketChaos(),
      newHighRate: this.marketInfo.newHighRate,
      newLowRate: this.marketInfo.newLowRate,
      lastMarket15mAllGoodIndex: this.lastMarket15mAllGoodIndex(),
      lastMarket15mAllBadIndex: this.lastMarket15mAllBadIndex(),
      ifMarket15mAllGood3Days: this.ifMarket15mAllGood3Days(),
      ifMarket15mAllBad3Days: this.ifMarket15mAllBad3Days(),
      ifMarket15mAllGood4Days: this.ifMarket15mAllGood4Days(),
      ifMarket15mAllBad4Days: this.ifMarket15mAllBad4Days(),
      ifMarket15mAllGood5Days: this.ifMarket15mAllGood5Days(),
      ifMarket15mAllBad5Days: this.ifMarket15mAllBad5Days(),
      isMarketFakeGoodUp: this.isMarketFakeGoodUp(),
      isMarketFakeBadDown: this.isMarketFakeBadDown(),
      ifMarket15mKdjGood3Days: this.ifMarket15mKdjGood3Days(),
      ifMarket15mKdjBad3Days: this.ifMarket15mKdjBad3Days(),
      // kdj3天没有bad了
      ifMarket15mKdjNoBad3Days: this.ifMarket15mKdjNoBad3Days(),
      ifMarket15mKdjNoGood3Days: this.ifMarket15mKdjNoGood3Days(),
      marketKdj1dGoodDays: this.marketKdj1dGoodDays(),
      marketKdj1dBadDays: this.marketKdj1dBadDays(),
      marketMacd1dGoodDays: this.marketMacd1dGoodDays(),
      marketMacd1dBadDays: this.marketMacd1dBadDays(),
      marketEma1dGoodDays: this.marketEma1dGoodDays(),
      marketEma1dBadDays: this.marketEma1dBadDays(),
      marketKdj4hGoodDays: this.marketKdj4hGoodDays(),
      marketKdj4hBadDays: this.marketKdj4hBadDays(),
      marketMacd4hGoodDays: this.marketMacd4hGoodDays(),
      marketMacd4hBadDays: this.marketMacd4hBadDays(),
      marketEma4hGoodDays: this.marketEma4hGoodDays(),
      marketEma4hBadDays: this.marketEma4hBadDays(),
      marketKdj1hGoodDays: this.marketKdj1hGoodDays(),
      marketKdj1hBadDays: this.marketKdj1hBadDays(),
      marketMacd1hGoodDays: this.marketMacd1hGoodDays(),
      marketMacd1hBadDays: this.marketMacd1hBadDays(),
      marketEma1hGoodDays: this.marketEma1hGoodDays(),
      marketEma1hBadDays: this.marketEma1hBadDays(),
      marketKdj15mGoodDays: this.marketKdj15mGoodDays(),
      marketKdj15mBadDays: this.marketKdj15mBadDays(),
      marketMacd15mGoodDays: this.marketMacd15mGoodDays(),
      marketMacd15mBadDays: this.marketMacd15mBadDays(),
      marketEma15mGoodDays: this.marketEma15mGoodDays(),
      marketEma15mBadDays: this.marketEma15mBadDays(),
      market15mAllBadKeepDays: this.market15mAllBadKeepDays(),
      market15mAllGoodKeepDays: this.market15mAllGoodKeepDays(),
      lastMarket15mAllGood4DaysIndex: this.lastMarket15mAllGood4DaysIndex(),
      lastMarket15mAllBad4DaysIndex: this.lastMarket15mAllBad4DaysIndex()
    }
  },
  ifMarketInModel (list) {
    for (let i = 0; i < list.length; i++) {
      if (this.ifMarketModelLike(list[i])) {
        return true
      }
    }
    return false
  },
  // 快捷模式[0,1,-1]
  ifMarketModelLike (list) {
    if (typeof list === 'string') {
      list = JSON.parse(list)
    }
    let flag = true
    list.forEach((v, index) => {
      if (index === 0) {
        if (!this.ifMarket1dModelLike(v)) {
          flag = false
        }
      }
      if (index === 1) {
        if (!this.ifMarket4hModelLike(v)) {
          flag = false
        }
      }
      if (index === 2) {
        if (!this.ifMarket1hModelLike(v)) {
          flag = false
        }
      }
      if (index === 3) {
        if (!this.ifMarket15mModelLike(v)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket1dModelLike (list) {
    if (typeof list === 'string') {
      list = JSON.parse(list)
    }
    let flag = true
    const infoList = [
      [this.marketInfo.kdj1dBad, this.marketInfo.kdj1dGood],
      [this.marketInfo.macd1dBad, this.marketInfo.macd1dGood],
      [this.marketInfo.ema1dBad, this.marketInfo.ema1dGood]
    ]
    list.forEach((v, index) => {
      if (!checkMarketTag(infoList[index], v)) {
        flag = false
      }
    })
    return flag
  },
  ifMarket4hModelLike (list) {
    if (typeof list === 'string') {
      list = JSON.parse(list)
    }
    let flag = true
    const infoList = [
      [this.marketInfo.kdj4hBad, this.marketInfo.kdj4hGood],
      [this.marketInfo.macd4hBad, this.marketInfo.macd4hGood],
      [this.marketInfo.ema4hBad, this.marketInfo.ema4hGood]
    ]
    list.forEach((v, index) => {
      if (!checkMarketTag(infoList[index], v)) {
        flag = false
      }
    })
    return flag
  },
  ifMarket1hModelLike (list) {
    if (typeof list === 'string') {
      list = JSON.parse(list)
    }
    let flag = true
    const infoList = [
      [this.marketInfo.kdj1hBad, this.marketInfo.kdj1hGood],
      [this.marketInfo.macd1hBad, this.marketInfo.macd1hGood],
      [this.marketInfo.ema1hBad, this.marketInfo.ema1hGood]
    ]
    list.forEach((v, index) => {
      if (!checkMarketTag(infoList[index], v)) {
        flag = false
      }
    })
    return flag
  },
  ifMarket15mModelLike (list) {
    if (typeof list === 'string') {
      list = JSON.parse(list)
    }
    let flag = true
    const infoList = [
      [this.marketInfo.kdj15mBad, this.marketInfo.kdj15mGood],
      [this.marketInfo.macd15mBad, this.marketInfo.macd15mGood],
      [this.marketInfo.ema15mBad, this.marketInfo.ema15mGood]
    ]
    list.forEach((v, index) => {
      if (!checkMarketTag(infoList[index], v)) {
        flag = false
      }
    })
    return flag
  },
  indexIfMarketDownTrend () {
    if (this.marketInfo) {
      return btbAs.checkIfMarketDownTrend(this.marketInfo)
    }
    return false
  },
  indexIfMarketUpTrend () {
    if (this.marketInfo) {
      return btbAs.checkIfMarketUpTrend(this.marketInfo)
    }
    return false
  },
  // 没有方向的状态
  ifMarketNoDirection () {
    return this.ifMarketModelLike([
      [0, 'any', 'any'],
      [0, 0, 0],
      ['any', 0, 0],
      ['any', 'any', 0]
    ])
  },
  indexIfMarket15mDownTrend () {
    if (this.marketInfo) {
      const countModel15m = this.marketInfo.countModel15m || {}
      if (
        countModel15m.countKdjDown > (0.8 * this.marketInfo.asCount) &&
        countModel15m.countMacdDown > (0.8 * this.marketInfo.asCount)
      ) {
        return true
      }
    }
    return false
  },
  indexIfMarket15mUpTrend () {
    if (this.marketInfo) {
      const countModel15m = this.marketInfo.countModel15m || {}
      if (
        countModel15m.countKdjUp > (0.8 * this.marketInfo.asCount) &&
        countModel15m.countMacdUp > (0.8 * this.marketInfo.asCount)
      ) {
        return true
      }
    }
    return false
  },
  ifMarket1dAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1dGood && this.marketInfo.macd1dGood && this.marketInfo.ema1dGood) {
        return true
      }
    }
    return false
  },
  ifMarket1dHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1dGood || this.marketInfo.macd1dGood || this.marketInfo.ema1dGood) {
        return true
      }
    }
    return false
  },
  ifMarket1dHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1dBad || this.marketInfo.macd1dBad || this.marketInfo.ema1dBad) {
        return true
      }
    }
    return false
  },
  ifMarket1dAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1dBad && this.marketInfo.macd1dBad && this.marketInfo.ema1dBad) {
        return true
      }
    }
    return false
  },
  ifMarket1dMacdEmaHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1dGood || this.marketInfo.ema1dGood) {
        return true
      }
    }
    return false
  },
  ifMarket1dMacdEmaHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1dBad || this.marketInfo.ema1dBad) {
        return true
      }
    }
    return false
  },
  ifMarket1dMacdEmaAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1dGood && this.marketInfo.ema1dGood) {
        return true
      }
    }
    return false
  },
  ifMarket1dMacdEmaAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1dBad && this.marketInfo.ema1dBad) {
        return true
      }
    }
    return false
  },
  ifMarket4hHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj4hGood || this.marketInfo.macd4hGood || this.marketInfo.ema4hGood) {
        return true
      }
    }
    return false
  },
  ifMarket4hAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj4hGood && this.marketInfo.macd4hGood && this.marketInfo.ema4hGood) {
        return true
      }
    }
    return false
  },
  ifMarket4hHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj4hBad || this.marketInfo.macd4hBad || this.marketInfo.ema4hBad) {
        return true
      }
    }
    return false
  },
  ifMarket4hAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj4hBad && this.marketInfo.macd4hBad && this.marketInfo.ema4hBad) {
        return true
      }
    }
    return false
  },
  ifMarket4hMacdEmaAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd4hGood && this.marketInfo.ema4hGood) {
        return true
      }
    }
    return false
  },
  ifMarket4hMacdEmaAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd4hBad && this.marketInfo.ema4hBad) {
        return true
      }
    }
    return false
  },
  ifMarket4hMacdEmaHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd4hGood || this.marketInfo.ema4hGood) {
        return true
      }
    }
    return false
  },
  ifMarket4hMacdEmaHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd4hBad || this.marketInfo.ema4hBad) {
        return true
      }
    }
    return false
  },
  ifMarket1hAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1hGood && this.marketInfo.macd1hGood && this.marketInfo.ema1hGood) {
        return true
      }
    }
    return false
  },
  ifMarket1hHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1hGood || this.marketInfo.macd1hGood || this.marketInfo.ema1hGood) {
        return true
      }
    }
    return false
  },
  ifMarket1hHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1hBad || this.marketInfo.macd1hBad || this.marketInfo.ema1hBad) {
        return true
      }
    }
    return false
  },
  ifMarket1hAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj1hBad && this.marketInfo.macd1hBad && this.marketInfo.ema1hBad) {
        return true
      }
    }
    return false
  },
  ifMarket1hMacdEmaHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1hGood || this.marketInfo.ema1hGood) {
        return true
      }
    }
    return false
  },
  ifMarket1hMacdEmaHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1hBad || this.marketInfo.ema1hBad) {
        return true
      }
    }
    return false
  },
  ifMarket1hMacdEmaAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1hGood && this.marketInfo.ema1hGood) {
        return true
      }
    }
    return false
  },
  ifMarket1hMacdEmaAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd1hBad && this.marketInfo.ema1hBad) {
        return true
      }
    }
    return false
  },
  ifMarket15mAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj15mGood && this.marketInfo.macd15mGood && this.marketInfo.ema15mGood) {
        return true
      }
    }
    return false
  },
  ifMarket15mHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj15mGood || this.marketInfo.macd15mGood || this.marketInfo.ema15mGood) {
        return true
      }
    }
    return false
  },
  ifMarket15mHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj15mBad || this.marketInfo.macd15mBad || this.marketInfo.ema15mBad) {
        return true
      }
    }
    return false
  },
  ifMarket15mAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.kdj15mBad && this.marketInfo.macd15mBad && this.marketInfo.ema15mBad) {
        return true
      }
    }
    return false
  },
  ifMarket15mMacdEmaHasGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd15mGood || this.marketInfo.ema15mGood) {
        return true
      }
    }
    return false
  },
  ifMarket15mMacdEmaHasBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd15mBad || this.marketInfo.ema15mBad) {
        return true
      }
    }
    return false
  },
  ifMarket15mMacdEmaAllGood () {
    if (this.marketInfo) {
      if (this.marketInfo.macd15mGood && this.marketInfo.ema15mGood) {
        return true
      }
    }
    return false
  },
  ifMarket15mMacdEmaAllBad () {
    if (this.marketInfo) {
      if (this.marketInfo.macd15mBad && this.marketInfo.ema15mBad) {
        return true
      }
    }
    return false
  },
  isMarketLittleGoodUp () {
    if (!(this.marketInfo.count1h5W10A20Days < 16 || this.marketInfo.count1h5W10A20Days > 32)) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    // 下跌和上涨有本质区别，上涨的时候ETH和btc是最早上线的，但是下跌的时候他们是最晚下线的
    if (
      // 这是刚启动的模式，4h的macd和1d的kdj先行
      (this.marketInfo.kdj1dGood && this.marketInfo.macd1dGood) &&
      countModel1d.count5W10A20 >= (0.5 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllGood() &&
      (this.marketInfo.ema1hGood)
    ) {
      return true
    }
    const countModel4h = this.marketInfo.countModel4h || {}
    if (
      // 基本都要kdj
      (this.marketInfo.kdj1dGood && this.marketInfo.macd1dGood && this.marketInfo.ema1dGood) &&
      (this.marketInfo.kdj4hGood && this.marketInfo.ema4hGood) &&
      (
        countModel4h.countMacdUp >= (0.75 * this.marketInfo.asCount) &&
        this.marketInfo.ema15mGood
      ) &&
      (this.marketInfo.macd1hGood && this.marketInfo.ema1hGood)
    ) {
      return true
    }
    return false
  },
  isMarketMiniGoodUp () {
    if (!this.isMarketLittleGoodUp()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dGood) &&
      countModel1d.count5W10A20 >= (0.5 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllGood() &&
      (this.marketInfo.ema1hGood && this.marketInfo.likeDuo && this.marketInfo.macd1hGood)
    ) {
      return true
    }
    return false
  },
  isMarketSmallGoodUp () {
    if (!this.isMarketLittleGoodUp()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dGood) &&
      countModel1d.count5W10A20 >= (0.5 * this.marketInfo.asCount) &&
      this.ifMarket4hAllGood() &&
      (this.marketInfo.kdj1hGood && this.marketInfo.ema1hGood && this.marketInfo.macd1hGood) &&
      this.ifMarket15mMacdEmaAllGood()
    ) {
      return true
    }
    return false
  },
  isMarketBigGoodUp () {
    if (!this.isMarketLittleGoodUp()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    // 下跌和上涨有本质区别，上涨的时候ETH和btc是最早上线的，但是下跌的时候他们是最晚下线的
    if (
      (this.marketInfo.kdj1dGood) &&
      countModel1d.count5W10A20 >= (0.5 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllGood() &&
      this.ifMarket1hMacdEmaAllGood() &&
      this.marketInfo.ema15mGood
    ) {
      return true
    }
    return false
  },
  isMarketLittleBadDown () {
    if (!(this.marketInfo.count1h5W10A20Days < 16 || this.marketInfo.count1h5W10A20Days > 32)) {
      return false
    }
    // 下跌和上涨有本质区别，上涨的时候ETH和btc是最早上线的，但是下跌的时候他们是最晚下线的
    const countModel4h = this.marketInfo.countModel4h || {}
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dBad && this.marketInfo.macd1dBad) &&
      countModel1d.count5W10B20 >= (0.75 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllBad() &&
      (this.marketInfo.ema1hBad)
    ) {
      return true
    }
    if (
      (this.marketInfo.kdj1dBad && this.marketInfo.macd1dBad && this.marketInfo.ema1dBad) &&
      (this.marketInfo.kdj4hBad && this.marketInfo.ema4hBad) &&
      (
        countModel4h.countMacdDown >= (0.75 * this.marketInfo.asCount) &&
        this.marketInfo.ema15mBad
      ) &&
      (this.marketInfo.macd1hBad && this.marketInfo.ema1hBad)
    ) {
      return true
    }
    return false
  },
  isMarketMiniBadDown () {
    if (!this.isMarketLittleBadDown()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dBad) &&
      countModel1d.count5W10B20 >= (0.75 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllBad() &&
      (this.marketInfo.ema1hBad && this.marketInfo.likeKong && this.marketInfo.macd1hBad)
    ) {
      return true
    }
    return false
  },
  isMarketSmallBadDown () {
    if (!this.isMarketLittleBadDown()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dBad) &&
      countModel1d.count5W10B20 >= (0.75 * this.marketInfo.asCount) &&
      this.ifMarket4hAllBad() &&
      (this.marketInfo.kdj1hBad && this.marketInfo.ema1hBad && this.marketInfo.macd1hBad) &&
      this.ifMarket15mMacdEmaAllBad()
    ) {
      return true
    }
    return false
  },
  isMarketBigBadDown () {
    if (!this.isMarketLittleBadDown()) {
      return false
    }
    const countModel1d = this.marketInfo.countModel1d || {}
    if (
      (this.marketInfo.kdj1dBad) &&
      countModel1d.count5W10B20 >= (0.75 * this.marketInfo.asCount) &&
      this.ifMarket4hMacdEmaAllBad() &&
      this.ifMarket1hMacdEmaAllBad() &&
      this.marketInfo.ema15mBad
    ) {
      return true
    }
    return false
  },
  isMarketChaos () {
    if (Math.abs(this.marketInfo.marketPriceChangePercent) < 1.2) {
      return true
    }
    return false
  },
  isMarketFakeGoodUp () {
    if (
      this.ifMarket4hAllGood() &&
      this.ifMarket1hAllGood() &&
      this.ifMarket15mAllGood() &&
      !this.marketInfo.kdj1dGood
    ) {
      return true
    }
  },
  isMarketFakeBadDown () {
    if (
      this.ifMarket4hAllBad() &&
      this.ifMarket1hAllBad() &&
      this.ifMarket15mAllBad() &&
      !this.marketInfo.kdj1dBad
    ) {
      return true
    }
  },
  lastMarket15mAllGoodIndex () {
    for (let i = 0; i < this.marketInfoList.length; i++) {
      const v = this.marketInfoList[i]
      if (v.kdj15mGood && v.macd15mGood && v.ema15mGood) {
        return i
      }
    }
    return 200
  },
  lastMarket15mAllBadIndex () {
    for (let i = 0; i < this.marketInfoList.length; i++) {
      const v = this.marketInfoList[i]
      if (v.kdj15mBad && v.macd15mBad && v.ema15mBad) {
        return i
      }
    }
    return 200
  },
  lastMarket15mAllGood4DaysIndex () {
    for (let i = 0; i < this.marketInfoList.length; i++) {
      if (this.ifMarket15mAllGoodDaysByIndex(i, 4)) {
        return i
      }
    }
    return 200
  },
  lastMarket15mAllBad4DaysIndex () {
    for (let i = 0; i < this.marketInfoList.length; i++) {
      if (this.ifMarket15mAllBadDaysByIndex(i, 4)) {
        return i
      }
    }
    return 200
  },
  ifMarket15mAllBadDaysByIndex (index, days) {
    let flag = true
    for (let i = index; i < (index + days); i++) {
      const v = this.marketInfoList[i]
      if (!v) {
        flag = false
        return flag
      }
      if (!(v.kdj15mBad && v.macd15mBad && v.ema15mBad)) {
        flag = false
      }
    }
    return flag
  },
  ifMarket15mAllGoodDaysByIndex (index, days) {
    let flag = true
    for (let i = index; i < (index + days); i++) {
      const v = this.marketInfoList[i]
      if (!v) {
        flag = false
        return flag
      }
      if (!(v.kdj15mGood && v.macd15mGood && v.ema15mGood)) {
        flag = false
      }
    }
    return flag
  },
  market15mAllGoodKeepDays () {
    let days = 0
    for (let i = 0; i < this.marketInfoList.length; i++) {
      const v = this.marketInfoList[i]
      if (!(v.kdj15mGood && v.macd15mGood && v.ema15mGood)) {
        return days
      } else {
        days++
      }
    }
    return days
  },
  market15mAllBadKeepDays () {
    let days = 0
    for (let i = 0; i < this.marketInfoList.length; i++) {
      const v = this.marketInfoList[i]
      if (!(v.kdj15mBad && v.macd15mBad && v.ema15mBad)) {
        return days
      } else {
        days++
      }
    }
    return days
  },
  ifMarket15mAllGood3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (!(v.kdj15mGood && v.macd15mGood && v.ema15mGood)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mAllBad3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (!(v.kdj15mBad && v.macd15mBad && v.ema15mBad)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mAllGood4Days () {
    let flag = true
    if (this.marketInfoList.length < 4) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 4) {
        if (!(v.kdj15mGood && v.macd15mGood && v.ema15mGood)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mAllBad4Days () {
    let flag = true
    if (this.marketInfoList.length < 4) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 4) {
        if (!(v.kdj15mBad && v.macd15mBad && v.ema15mBad)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mAllGood5Days () {
    let flag = true
    if (this.marketInfoList.length < 5) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 5) {
        if (!(v.kdj15mGood && v.macd15mGood && v.ema15mGood)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mAllBad5Days () {
    let flag = true
    if (this.marketInfoList.length < 5) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 5) {
        if (!(v.kdj15mBad && v.macd15mBad && v.ema15mBad)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mKdjGood3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (!(v.kdj15mGood)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mKdjBad3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (!(v.kdj15mBad)) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mKdjNoBad3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (v.kdj15mBad) {
          flag = false
        }
      }
    })
    return flag
  },
  ifMarket15mKdjNoGood3Days () {
    let flag = true
    if (this.marketInfoList.length < 3) {
      return false
    }
    this.marketInfoList.forEach((v, index) => {
      if (index < 3) {
        if (v.kdj15mGood) {
          flag = false
        }
      }
    })
    return flag
  },
  countMarketDays (key) {
    let days = 0
    for (let i = 0; i < this.marketInfoList.length; i++) {
      const v = this.marketInfoList[i]
      if (v[key]) {
        days++
      } else {
        return days
      }
    }
    return days
  },
  marketKdj1dGoodDays () {
    return this.countMarketDays('kdj1dGood')
  },
  marketKdj1dBadDays () {
    return this.countMarketDays('kdj1dBad')
  },
  marketMacd1dGoodDays () {
    return this.countMarketDays('macd1dGood')
  },
  marketMacd1dBadDays () {
    return this.countMarketDays('macd1dBad')
  },
  marketEma1dGoodDays () {
    return this.countMarketDays('ema1dGood')
  },
  marketEma1dBadDays () {
    return this.countMarketDays('ema1dBad')
  },
  marketKdj4hGoodDays () {
    return this.countMarketDays('kdj4hGood')
  },
  marketKdj4hBadDays () {
    return this.countMarketDays('kdj4hBad')
  },
  marketMacd4hGoodDays () {
    return this.countMarketDays('macd4hGood')
  },
  marketMacd4hBadDays () {
    return this.countMarketDays('macd4hBad')
  },
  marketEma4hGoodDays () {
    return this.countMarketDays('ema4hGood')
  },
  marketEma4hBadDays () {
    return this.countMarketDays('ema4hBad')
  },
  marketKdj1hGoodDays () {
    return this.countMarketDays('kdj1hGood')
  },
  marketKdj1hBadDays () {
    return this.countMarketDays('kdj1hBad')
  },
  marketMacd1hGoodDays () {
    return this.countMarketDays('macd1hGood')
  },
  marketMacd1hBadDays () {
    return this.countMarketDays('macd1hBad')
  },
  marketEma1hGoodDays () {
    return this.countMarketDays('ema1hGood')
  },
  marketEma1hBadDays () {
    return this.countMarketDays('ema1hBad')
  },
  marketKdj15mGoodDays () {
    return this.countMarketDays('kdj15mGood')
  },
  marketKdj15mBadDays () {
    return this.countMarketDays('kdj15mBad')
  },
  marketMacd15mGoodDays () {
    return this.countMarketDays('macd15mGood')
  },
  marketMacd15mBadDays () {
    return this.countMarketDays('macd15mBad')
  },
  marketEma15mGoodDays () {
    return this.countMarketDays('ema15mGood')
  },
  marketEma15mBadDays () {
    return this.countMarketDays('ema15mBad')
  }
}

module.exports = MarketMachine
